﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EarringsBackEnd
{
    public partial class frmSupplier : Form
    {
        private MasterDataManagement _dtMgmt = null;
        private List<PropertiesValue> _propValue = null;
        private string _styleId = string.Empty;
        private bool _chkEmptyValue = false;
        private int _proviceId;
        private int _districtId;
        private int _subDistrictId;
        private int _supplierId;
        private string[] _gridHearder = new string [] {"ลำดับที่", "รูปแบบต่างหู", "จำนวนวันที่ใช้สั่งซื้อ" };
        

        public frmSupplier()
        {
            InitializeComponent();
        }

        private void frmSupplier_Load(object sender, EventArgs e)
        {
            //Load Data to ComboBox
            //Style
            this.GetEarringsStyle();
            this.InitializeDataGridView();
        }

        private void GetEarringsStyle()
        {
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _propValue = new List<PropertiesValue>();
            _propValue = (List<PropertiesValue>)_dtMgmt.GetMasterEarringConfig<PropertiesValue>(GlobalValue.SHRTYP_ERSTY);

            PropertiesValue styVal = new PropertiesValue();
            styVal.KeyId = "0";
            styVal.Name = "--กรุณาเลือกรูปแบบต่างหู--";
            _propValue.Insert(0, styVal);

            cboStyle.DataSource = _propValue;
            cboStyle.ValueMember = "KeyId";
            cboStyle.DisplayMember = "Name";
            cboStyle.SelectedIndex = 0;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.CheckEmptyValue();

            if (_chkEmptyValue)
            {
                MessageBox.Show("กรุณากรอกข้อมูลให้ครบถ้วน");
            }
            else
            {
                //_nextKeyId = ValueUtil.GetNextKeyEarrings(_styleId, shortName);

                this.SaveData();
            }
        }

        private void CheckEmptyValue()
        {
            if (string.IsNullOrEmpty(txtSupName.Text) || string.IsNullOrEmpty(txtAddress.Text)
                || string.IsNullOrEmpty(txtZipCode.Text) || string.IsNullOrEmpty(txtTelephone.Text)
                || string.IsNullOrEmpty(txtFax.Text) || string.IsNullOrEmpty(txtEmail.Text)
                || string.IsNullOrEmpty(_proviceId.ToString()) || string.IsNullOrEmpty(_districtId.ToString())
                || string.IsNullOrEmpty(_subDistrictId.ToString()))
            {
                _chkEmptyValue = true;
            }
            else
            {
                _chkEmptyValue = false;
            }
        }

        private void SaveData()
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@Name",txtSupName.Text),
                    new SqlParameter("@Address", txtAddress.Text),
                    new SqlParameter("@Province", _proviceId),
                    new SqlParameter("@District", _districtId),
                    new SqlParameter("@SubDistrict", _subDistrictId),
                    new SqlParameter("@ZipCode", txtZipCode.Text),
                    new SqlParameter("@TelePhone", txtTelephone.Text),
                    new SqlParameter("@Fax", txtFax.Text),
                    new SqlParameter("@Email", txtEmail.Text),
                };
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _supplierId = _dtMgmt.SaveSupplier(sqlParams);
        }

        private void InitializeDataGridView()
        {
            for (int intIndex = 0; intIndex < 3 ; intIndex++)
            {
                dtgvSupEarring.Columns[intIndex].HeaderText = _gridHearder[intIndex];
            }
            
            dtgvSupEarring.Columns[0].Width = 70;
            dtgvSupEarring.Columns[1].Width = 120;
            dtgvSupEarring.Columns[2].Width = 120;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int gridrow = dtgvSupEarring.Rows.Count;

            dtgvSupEarring.Rows.Add(gridrow,cboStyle.Text, txtDay.Text);
        }
    }
}
